// pages/used_list/used_list.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    low_img: "/statics/img/must_low_price.png",
    bow_price: "/statics/img/bow_price.png",
    cart_num: app.globalData.cart_num,
    spe_show: false,
    tabBar: app.globalData.tabBar,
    activeIndex: 0,
    list_name: "常用清单",
    used_title: [],
    txtStyle: '',
    styleText: '',
    seat: '',
    isload: false,
    balls: [
      { isShow: false, animationData: null },
      { isShow: false, animationData: null }
    ],
    is_used: true,
    nav_scroll: 0,
    order_num:0
  },
  // delBtnWidth:0,
  startX: 0,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // wx.createSelectorQuery().select('#del').boundingClientRect(function (rect) {
    //   that.delBtnWidth=rect.width
    // }).exec()
  },
  active: function (e) {
    var that = this
    var width = e.currentTarget.offsetLeft / e.currentTarget.id
    this.setData({
      activeIndex: e.currentTarget.id,
      nav_scroll: e.currentTarget.offsetLeft - width * 2,
    })
    wx.createSelectorQuery().select('#list_' + e.currentTarget.id).boundingClientRect(function (rect) {
      var scrollTop = that.scrollTop || 0
      console.log(scrollTop)
      wx.pageScrollTo({
        scrollTop: rect.top + scrollTop - that.nav_height,//滚动距离计算
      })
    }).exec()
  },
  onPageScroll: function (e) {
    var that = this
    var used_title = this.data.used_title
    this.scrollTop = e.scrollTop
    var three_left = that.data.three_left || 0
    for (let i in used_title) {
      wx.createSelectorQuery().select('#list_' + i).boundingClientRect(function (res) {
        if (res.top >= 90 && res.top <= 200) {
          wx.createSelectorQuery().select('.nav_' + i).boundingClientRect(function (rect) {
            var offsetleft = rect.left + three_left
            var width = offsetleft / rect.id  
            that.setData({
              activeIndex: i,
              nav_scroll: offsetleft - width * 2
            })
          }).exec()
        }
      }).exec()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    app.ajax({
      url: app.host + 'mall/Freqlist/recgoods',
      success: function (res) {
        var used_title = res.data.data
        for (var i in used_title) {
          for (var j in used_title[i].list) {
            used_title[i].list[j].par_index = i
            used_title[i].list[j].index = j
            used_title[i].list[j].show_spece = true
            used_title[i].list[j].choose_text = "选规格"
          }
        }
        that.setData({
          used_title: res.data.data,
          isload: true
        })
        wx.createSelectorQuery().select('.nav_box').boundingClientRect(function (rect) {
          that.nav_top = rect.top
          that.nav_height = rect.height
        }).exec()
        wx.createSelectorQuery().selectViewport().scrollOffset(function (res) {
          that.scrollTop = res.scrollTop  // 节点的竖直滚动位置
        }).exec()
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    })
    app.cart_num(this)
    app.new_order(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  choose_G: function (e) {
    var index = e.currentTarget.dataset.index
    var par_index = e.currentTarget.dataset.par_index
    var used_title = this.data.used_title
    if (used_title[par_index].list[index].show_spece) {
      used_title[par_index].list[index].show_spece = false
      used_title[par_index].list[index].choose_text = "收起"
    } else {
      used_title[par_index].list[index].show_spece = true
      used_title[par_index].list[index].choose_text = "选规格"
    }
    this.setData({
      used_title: used_title
    })
  },

  add_cart: function (e) {
    app.ball_animate(e, this)
    var that = this
    var used_title = this.data.used_title
    var specval_id = e.currentTarget.dataset.specval_id
    for (var i in used_title) {
      for (var j in used_title[i].list) {
        if (used_title[i].list[j].specval_id == specval_id) {
          used_title[i].list[j].loading = true
        }
        for (var k in used_title[i].list[j].list) {
          if (used_title[i].list[j].list[k].specval_id == specval_id) {
            used_title[i].list[j].list[k].loading = true
            break
          }
        }
      }
    }
    this.setData({
      used_title: used_title
    })
    app.add_cart(e, this, function () {
      var used_title = that.data.used_title
      for (var i in used_title) {
        for (var j in used_title[i].list) {
          if (used_title[i].list[j].specval_id == specval_id) {
            used_title[i].list[j].loading = false
          }
          for (var k in used_title[i].list[j].list) {
            if (used_title[i].list[j].list[k].specval_id == specval_id) {
              used_title[i].list[j].list[k].loading = false
              break
            }
          }
        }
      }
      that.setData({
        used_title: used_title
      })
    });
  },

  del_freq: function (e) {
    var used_title = this.data.used_title
    var that = this
    app.ajax({
      url: app.host + 'mall/Freqlist/del',
      data: {
        freq_id: e.currentTarget.dataset.freq_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: '操作成功',
          })
        }
        for (let i in used_title) {
          for (let j in used_title[i].list) {
            if (used_title[i].list[j].freq_id == e.currentTarget.dataset.freq_id) {
              used_title[i].list.splice(j, 1)
              if (used_title[i].list.length == 0) {
                used_title.splice(i, 1)
              }
            }
          }
        }
        that.setData({
          used_title: used_title
        })
      },
    });
  },
})